import { Button, Popconfirm, Space, Tag, Tooltip } from 'antd';
import dayjs from 'dayjs';

export const columns = (editRuler, deleteRule, updateRuleStatus) => [
  {
    title:'index',
    dataIndex: 'index',
    key: 'index',
    width: 60,
    render: (_, { status },index) => <>{index+1}</>
  },
  // {
  //   title: 'id',
  //   dataIndex: 'uuid',
  //   key: 'uuid',
  //   width: 120,
  // },
  {
    title: '名称',
    dataIndex: 'name',
    key: 'name',
    width: 150,
  },
  {
    title: '状态',
    key: 'status',
    dataIndex: 'status',
    width: 80,
    render: (_, { status }) => (
      <Tag color={KvMap[status]?.color}>
        {KvMap[status]?.name || '-'}
      </Tag>
    ),
  },
  {
    title: "是否测试",
    dataIndex: 'isTest',
    key: "isTest",
    width: 80,
    render: (_, { isTest }) => (
      <Tag color={IS_TEST_MAP[isTest]?.color}>
        {IS_TEST_MAP[isTest]?.name || '-'}
      </Tag>
    ),
  },
  {
    title: "数据源类型",
    dataIndex: "datasourceType",
    key: "datasourceType",
    width: 100,
    render: (_, { datasourceType }) => (
      <Tag>{DATASOURCE_TYPE_MAP[datasourceType]}</Tag>
    )
  },
  {
    title: '数据源',
    dataIndex: 'datasourceName',
    key: 'datasourceName',
    width: 150,
    render: (text, { datasourceUuid }) => <span>{text || datasourceUuid || '-'}</span>,
  },
  {
    title: '告警群',
    dataIndex: 'alarmPlatforms',
    key: 'alarmPlatforms',
    width: 100,
    render: (arr) => <>{(arr && arr.length) ? arr.map(item => <Tag type='warning'>{item.name}</Tag>) : '-'}</>,
  },
  {
    title: '规则内容',
    dataIndex: 'sqlContent',
    key: 'sqlContent',
    width: 200,
    render: (_) => _ ?
      <Tooltip title={_} trigger="click" defaultOpen={false}>
        {_.substring(0, 30)}
      </Tooltip>
      : "-"
  },
  {
    title: '规则类型',
    dataIndex: 'ruleType',
    key: 'ruleType',
    width: 100,
    render: (_, record) => {
      return RULE_MAP[_]
    }
  },
  {
    title: '准确时间',
    dataIndex: 'timeValue',
    key: 'timeValue',
    width: 80,
    render: (_) => _ ? _ : "-"
  },
  {
    title: '时间单位',
    dataIndex: 'timeUnit',
    key: 'timeUnit',
    width: 80,
    render: (_) => {
      const map = {
        // 0:'分钟',
        [1 * 60 * 1000]: '分钟',
        [1 * 60 * 60 * 1000]: '小时',
        [24 * 1 * 60 * 60 * 1000]: '天',
        [7 * 24 * 1 * 60 * 60 * 1000]: '周',
      }
      return _ ? map[_] : "-"
    }
  },

  {
    title: '创建人',
    dataIndex: 'createdBy',
    key: 'createdBy',
    width: 130,
    render: (_) => _ ? _ : "-"
  },
  {
    title: '创建时间',
    dataIndex: 'createdAt',
    key: 'createdAt',
    width: 130,
    render: (_, record) => {
      return _ ? dayjs(_).format('YYYY-MM-DD HH:mm:ss') : '-'
    }
  },
  {
    title: '更新人',
    dataIndex: 'updatedBy',
    key: 'updatedBy',
    width: 130,
    render: (_) => _ ? _ : "-"
  },
  {
    title: '更新时间',
    dataIndex: 'updatedAt',
    key: 'updatedAt',
    width: 130,
    render: (_, record) => {
      return _ ? dayjs(_).format('YYYY-MM-DD HH:mm:ss') : '-'
    }
  },
  {
    title: '操作',
    key: 'operate',
    width: 200,
    fixed: 'right',
    align:'center',
    render: (_, record) => (
      <Space size="middle">
        <Button type='primary' size='small' onClick={() => editRuler(record)}>编辑</Button>
        <Popconfirm
          title={!record.status ? '开启当前规则' : '暂停当前规则'}
          description={!record.status ? '开启当前规则' : '暂停当前规则'}
          onConfirm={() => updateRuleStatus(!record.status ? 1 : 0, record)}
          okText="确定"
          cancelText="取消"
        > <Button size='small' type={!record.status ? 'primary' : 'dashed'}>{!record.status ? '开启' : '暂停'}</Button>
        </Popconfirm>
        <Popconfirm
          title="删除当前规则"
          description="继续删除当前规则?"
          onConfirm={() => deleteRule(record.uuid)}
          okText="确定"
          cancelText="取消"
        >
          <Button size='small' danger >删除</Button>
        </Popconfirm>
      </Space >
    ),
  },
];

export const KvMap = {
  true: {
    color: 'green',
    name: '开启',
  },
  false: {
    color: 'red',
    name: '关闭',
  },
}

export const IS_TEST_MAP = {
  false: {
    color: 'red',
    name: '正式',
  },
  true: {
    color: 'green',
    name: '测试',
  },
}


export const DATASOURCE_TYPE_LIST = [
  {
    value: '1',
    key: 1,
    label: "SQL"
  },
  {
    value: '2',
    key: 2,
    label: "ES"
  }
]

export const DATASOURCE_TYPE_MAP = {
  '1': "SQL",
  '2': "ES"
}

export const ES_DATASOURCE_LIST = [
  {
    value: 'sys_client_log',
    key: 1,
    name: "sys_client_log"
  }
]

export const RULE_LIST = [
  {
    value: 4,
    label: "SQL解析告警"
  },
  {
    value: 3,
    label: "ES解析告警"
  },
  // {
  //   value: 1,
  //   label: "字段值缺失"
  // },
  // {
  //   value: 2,
  //   label: "周同比异常"

  // }
]

export const RULE_MAP = {
  1: "字段值缺失",
  2: "周同比异常",
  3: "ES解析告警",
  4: "SQL解析告警"
}